<?php

require_once(dirname(__FILE__) . "/x_session_lib/x_session_store.php");
require_once(dirname(__FILE__) . "/x_session_lib/x_session.base.php");
require_once(dirname(__FILE__) . "/x_session.conf.php");

/**
 * An altenative for the std. session_*() functions
 *
 * @todo remove $_SESSION["x_session"] modifying (this array element should be deleted from $_SESSION
 * when starting session and restored on writing it back to disk).
 */
class x_session extends x_session_base
{

	/**
	 * @var x_session_conf
	 */
	protected $conf;
	
	private $session_cookie_path = null;
	
	private $session_cookie_domain = null;

	/**
	 * @param string $session_store_path - session storage path (i. e. /www/domain.ru/sessions
	 * 		or ../../sessions (without the terminal slash)
	 * @param x_session_conf $x_session_conf - optional config
	 * @param string $session_cookie_path - cookie path (i. e. for the site located at http://domain.ru/
	 * 		it should be '/', for the site at http://domain.ru/folder/ it should be '/folder/'. Default
	 * 		is slash '/'
	 * @param string $session_cookie_domain - cookie domain (i. e. domain.ru or .domain.ru for extending
	 * 		the cookie to subdomains). Default is current domain
	 * @param boolean $write_begin - whether to keep the session opened for writing data. It is not
	 * 		recommended to hold opened sessions for a long time, because of file locking and possible delay
	 * 		in page load. Use instead session_write_begin(), session_write_commit() and
	 * 		session_write_rollback() methods. If you keep the session opened, you should submit changes in
	 * 		$_SESSION using session_write_commit()
	 */
	public function __construct($session_store_path, x_session_conf $x_session_conf = null, $session_cookie_path = null, $session_cookie_domain = null, $write_begin = false, $create_new_session = true)
	{
		parent::__construct($session_store_path);
		$this->session_cookie_path = $session_cookie_path;

		if ($x_session_conf)
		{
			$this->conf = $x_session_conf;
		}
		else
		{
			$this->conf = new x_session_conf();
		}

		$is_authed = $this->try_auth_over_cookie($write_begin);

		if (!$is_authed and $create_new_session)
		{
			$this->create_new_session($this->session_cookie_path, $this->session_cookie_domain, $write_begin);
		}
	}

	public function check_session_is_created($write_begin = false)
	{
		if (is_null($this->session_id))
		{
			$this->create_new_session($this->session_cookie_path, $this->session_cookie_domain, $write_begin);
		}
	}

}

?>